package com.snail.billing.fragment;

import org.json.JSONObject;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.android.os.Page;
import com.android.util.ResUtil;
import com.snail.billing.DataCache;
import com.snail.billing.PaymentConst;
import com.snail.billing.Resource;
import com.snail.billing.data.PaymentLogic;
import com.snail.billing.json.JsonCards;
import com.tencent.mm.sdk.modelpay.PayReq;

public class OrderFragment extends Page implements View.OnClickListener {

	private TextView textTitle;
	private View buttonBack;

	private TextView textOrderId;
	private TextView textProductName;
	private TextView textAccount;
	private TextView textAreaName;
	private TextView textMoney;
	private TextView textPrice;
	public View buttonPay;

	@Override
	public int createLayoutId() {
		return ResUtil.getLayoutId(Resource.layout.snailbilling_pay_order_activity);
	}

	@Override
	public void onCreate() {
		super.onCreate();
		textTitle = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_text));
		textTitle.setText(DataCache.getInstance().paymentParams.platformName);
		buttonBack = findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_button_back));
		buttonBack.setOnClickListener(this);
		//
		textOrderId = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_pay_order_text_order_id));
		textProductName = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_pay_order_text_product_name));
		textAccount = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_pay_order_text_account));
		textAreaName = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_pay_order_text_areaname));
		textMoney = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_pay_order_text_money));
		textPrice = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_pay_order_text_price));
		buttonPay = findViewById(ResUtil.getViewId(Resource.id.snailbilling_pay_order_button_pay));
		DataCache cache = DataCache.getInstance();
		try {
			JSONObject imprestOrder = new JSONObject(cache.paymentParams.orderResponse).getJSONObject("imprestOrder");
			String orderNo = imprestOrder.getString("orderNo");
			textOrderId.setText("订单号为: " + orderNo);
			textProductName.setText("充值产品: " + formatName(cache.paymentParams.card.getCardName()));
			textAccount.setText("充值账号: " + cache.importParams.account);
			if (!TextUtils.isEmpty(cache.paymentParams.areaId)) {
				textAreaName.setText("充值区服: " + cache.paymentParams.areaName);
			} else {
				textAreaName.setVisibility(View.GONE);
			}
			JsonCards.Card card = cache.paymentParams.card;
			textMoney.setText("充值金额: " + card.getMoney() + card.getCurrencyName());
			textPrice.setText("应付金额: " + card.getPrice() + "元");
		} catch (Exception e) {
			e.printStackTrace();
		}
		buttonPay.setOnClickListener(this);
	}

	private String formatName(String productName) {
		char[] chars = productName.toCharArray();
		int index = chars.length;
		for (int i = 0; i < chars.length; i++) {
			short s = (short) chars[i];
			if (48 <= s && s <= 57) {
				index = i;
				break;
			}
		}
		return productName.substring(0, index);
	}

	@Override
	public void onClick(View v) {
		if (v.equals(buttonBack)) {
			getPageManager().backward();
		} else if (v.equals(buttonPay)) {
			PaymentLogic paymentLogic = new PaymentLogic(getPageManager(), PaymentLogic.STATE_IMPREST);
			DataCache cache = DataCache.getInstance();
			int platformId = cache.paymentParams.platformId;
			String orderResponse = cache.paymentParams.orderResponse;
			try {
				if (platformId == PaymentConst.TYPE_ALIPAY_APP) {
					JSONObject paymentParams = new JSONObject(orderResponse).getJSONObject("paymentParams");
					final String source = paymentParams.getString("source");
					//
					paymentLogic.alipayApp(source);
					return;
				}
				if (platformId == PaymentConst.TYPE_UPOMP2) {
					JSONObject paymentParams = new JSONObject(orderResponse).getJSONObject("paymentParams");
					String unionMobilePayImprestSubmit = paymentParams.getString("unionMobilePayImprestSubmit");
					byte[] to_upomp = unionMobilePayImprestSubmit.getBytes();
					//
					paymentLogic.upompApp(to_upomp);
					return;
				}
				if (platformId == PaymentConst.TYPE_ALIPAY_WEB) {
					paymentLogic.alipayWeb();
					return;
				}
				if (platformId == PaymentConst.TYPE_TENPAY) {
					paymentLogic.tenpayWeb();
					return;
				}
				if (platformId == PaymentConst.TYPE_WECHAT) {
					JSONObject paymentParams = new JSONObject(orderResponse).getJSONObject("paymentParams");
					PayReq payReq = new PayReq();
					payReq.appId = paymentParams.getString("appid");
					payReq.partnerId = paymentParams.getString("partnerid");
					payReq.prepayId = paymentParams.getString("prepayid");
					payReq.nonceStr = paymentParams.getString("noncestr");
					payReq.timeStamp = paymentParams.getString("timestamp");
					payReq.packageValue = "Sign=WXPay";
					payReq.sign = paymentParams.getString("sign");
					//
					paymentLogic.wxpayApp(payReq);
					return;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
